﻿#region ... Copyright Notice ...
/*
   Copyright 2008 Tyler Jensen

	Author: Tyler Jensen    http://www.tsjensen.com/blog

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
#endregion

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Xml.Linq;
using NLog;

namespace Atrax.Logging
{
	public static class Log
	{
		static object locker = new object();
		static MyLogger m_applog = null;
		static MyLogger m_acctlog = null;
		static MyLogger m_debuglog = null;

		static Log()
		{
			if (m_applog == null || m_acctlog == null || m_debuglog == null) //initialize
			{
				m_applog = new MyLogger("applog", LogLevel.Info);
				m_acctlog = new MyLogger("acctlog", LogLevel.Info);
				m_debuglog = new MyLogger("debuglog", LogLevel.Debug);
			}
		}

		public static void Debug(string message)
		{
			Debug("000", message);
		}

		public static void Debug(string eventId, string message, Exception e)
		{
			message += " | " + ExceptionToString(e);
			Debug(eventId, message);
		}

		public static void Debug(string eventId, Exception e)
		{
			string message = ExceptionToString(e);
			Debug(eventId, message);
		}

		public static void Debug(string eventId, string message)
		{
			m_debuglog.WriteMessage(eventId, message);
		}

		public static void Acct(string eventId, string message)
		{
			m_acctlog.WriteMessage(eventId, message);
		}

		public static void App(string eventId, string message, Exception e)
		{
			message += " | " + ExceptionToString(e);
			App(eventId, message);
		}

		public static void App(string eventId, Exception e)
		{
			string message = ExceptionToString(e);
			App(eventId, message);
		}

		public static void App(string eventId, string message)
		{
			m_applog.WriteMessage(eventId, message);
		}

		static string ExceptionToString(Exception e)
		{
			return "Exception: " + e.ToString().Replace("\n", " // ").Replace("\r", "");
		}
	}

	class MyLogger
	{
		Logger _logger;
		LogLevel _level;

		public MyLogger(string name, LogLevel level)
		{
			_logger = LogManager.GetLogger(name);
			_level = level;
		}

		public void WriteMessage(string eventID, string message)
		{
			///
			/// create log event from the passed message
			/// 
			LogEventInfo logEvent = new LogEventInfo(_level, _logger.Name, string.Format("[{0}] {1}", eventID, message));

			//
			// set event-specific context parameter
			// this context parameter can be retrieved using ${event-context:EventID}
			//
			logEvent.Context["EventID"] = eventID;

			// 
			// Call the Log() method. It is important to pass typeof(MyLogger) as the
			// first parameter. If you don't, ${callsite} and other callstack-related 
			// layout renderers will not work properly.
			//

			_logger.Log(typeof(MyLogger), logEvent);
		}
	}

}
